//
//  TurnCircleTableView.m
//  头像简单制作
//
//  Created by 宋振 on 2018/9/17.
//  Copyright © 2018年 夜幕繁华. All rights reserved.
//

#define kPadding                    15

//设置比例
#define kScreenW    [UIScreen mainScreen].bounds.size.width



#define kScale(num) (num)*(kScreenW/375.0)



#import "TurnCircleTableView.h"
#import "Masonry.h"
#import "XLPlayExplainView.h"
#import "XLWheelFortunModel.h"
#import "XLWinningView.h"
#import "XLHomeViewModel.h"
#import "XLWheelRecordModel.h"
#import "XLUserInviteVC.h"

@interface TurnCircleTableView ()<CAAnimationDelegate>

//大背景
@property (nonatomic, strong) UIView *  groundView;

//跑马灯背景
@property (nonatomic, strong) UIView *  rabaoView;

//跑马灯
@property (nonatomic, strong) UILabel *  rabaoLbl;

//跑马灯
@property (nonatomic, strong) UILabel *  nextRabaoLbl;

// 转盘背景
@property (nonatomic,strong) UIImageView * roundImgView;

//点击按钮
@property (nonatomic, strong) UIButton *  clickBtn;

//顶部图片显示
@property (nonatomic, strong) UIImageView *  topImgView;

@property (nonatomic, strong) UIButton *  awardButton;

@property (nonatomic, strong) XLWheelFortunModel * model;
@property (nonatomic, strong) XLHomeViewModel * viewModel;



@end

@implementation TurnCircleTableView
{
    //获取广告
    NSInteger _index;
    //抽奖次数
    NSInteger _awardNum;
}

- (instancetype)initWithFrame:(CGRect)frame AndModel:(XLWheelFortunModel *)model
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        //绘制界面显示
        _model = model;
        [self initUI];
    }
    
    return self;
}

/**
 绘制界面显示
 */
- (void)initUI
{
    self.tag = 100000000;
    [self getWheelADVData];
    _awardNum = [self.model.XqWheelFortune.WheelTimes integerValue];
    self.backgroundColor = RGBA(0, 0, 0, 0.3);
    //背景设置
    [self addSubview:self.groundView];
    if(KScreenHeight > 800)
    {
        
    }
    [self.groundView mas_makeConstraints:^(MASConstraintMaker *make) {
        if(KScreenHeight > 800)
        {
            make.top.equalTo(self.mas_top).offset(kScale(88));
            make.bottom.equalTo(self.mas_bottom).offset(kScale(-103));
        }else{
            make.top.equalTo(self.mas_top).offset(kScale(48));
            make.bottom.equalTo(self.mas_bottom).offset(kScale(-83));
        }
        
        
        make.left.equalTo(self.mas_left).offset(kScale(15));
        make.right.equalTo(self.mas_right).offset(-kScale(15));
    }];
    
    //顶部图片标题
    [self.groundView addSubview:self.topImgView];
    
    [self.topImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.right.equalTo(self.groundView);
        make.height.mas_equalTo(kScale(82));
    }];
    
    //绘制播报显示
    [self setRunLabel];
    
    //绘制地盘
    [self.groundView addSubview:self.roundImgView];
    
    [self.roundImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.rabaoView.mas_bottom).offset(kScale(16));
        make.centerX.equalTo(self.groundView.mas_centerX);
        make.width.height.mas_equalTo(kScale(293));
    }];
    
    //点击按钮
    self.clickBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.clickBtn.frame = CGRectMake(0, 0, kScale(110), kScale(110));
        [self.clickBtn setImage:[UIImage imageNamed:@"pic_zhuanpan_zhizheng_n copy"] forState:(UIControlStateNormal)];
        [self.clickBtn addTarget:self action:@selector(startAnimation:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.groundView addSubview:self.clickBtn];
    
    [self.clickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.roundImgView);
        make.size.mas_equalTo(CGSizeMake(kScale(110), kScale(110)));
    }];
    
    
    //绘制承载奖品盘
    [self setRewardView];
 
    
    UIButton * closeButton = [[UIButton alloc]init];
    [closeButton setImage:IMAGE_NAMED(@"common_icon_delete_n") forState:UIControlStateNormal];
    [self.groundView addSubview:closeButton];
    
    kWeakSelf(self);
    [closeButton addTapBlock:^(UIButton *btn) {
        [weakself removeFromSuperview];
    }];
    
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(24, 24));
        make.top.equalTo(self.groundView.mas_top).offset(10);
        make.right.equalTo(self.groundView.mas_right).offset(-10);
    }];
    
    //玩法说明
    UIView * playExplainView = [[UIView alloc]init];
    playExplainView.backgroundColor = KClearColor;
    [self.groundView addSubview:playExplainView];
    
    [playExplainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rabaoView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(66, 17));
        make.right.equalTo(self.groundView.mas_right).offset(-15);
    }];
    UIImageView * explainIcon = [[UIImageView alloc]init];
    explainIcon.image = IMAGE_NAMED(@"pic_zhuanpan_wanfa_n copy");
    [playExplainView addSubview:explainIcon];
    
    [explainIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(12, 12));
        make.centerY.mas_equalTo(playExplainView.mas_centerY);
        make.left.equalTo(playExplainView.mas_left).offset(0);
    }];
    
    UILabel * explainLabel = [[UILabel alloc]init];
    explainLabel.text = @"玩法说明";
    explainLabel.textColor = RGB_HEX(0x999999);
    explainLabel.font = SYSTEMFONT(12);
    [playExplainView addSubview:explainLabel];
    
    [explainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.and.bottom.equalTo(playExplainView).insets(UIEdgeInsetsMake(0, 16, 0, 0));
    }];
    
    UIButton * explainButton = [[UIButton alloc]init];
    //玩法说明点击时间
    [explainButton addTapBlock:^(UIButton *btn) {
        XLPlayExplainView * view = [[XLPlayExplainView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, KScreenHeight)];
        [kAppWindow addSubview:view];
    }];
    [playExplainView addSubview:explainButton];
    [explainButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(playExplainView);
    }];
    
    if(_awardNum > 0)
    {
        [self.awardButton setTitle:NSStringFormat(@"免费抽奖 %lu 次",_awardNum) forState:UIControlStateNormal];
    }else
    {
        [self.awardButton setTitle:@"获取免费次数" forState:UIControlStateNormal];
    }
    [self.groundView addSubview:self.awardButton];
    
    //获取抽奖次数
    UIButton * getNumber = [[UIButton alloc]init];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"获取更多抽奖机会"];
    NSRange strRange = {0,[str length]};
    [str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
    [str addAttribute:NSStrokeColorAttributeName value:RGB_HEX(0x666666) range:strRange];
    [getNumber setAttributedTitle:str forState:UIControlStateNormal];
//    [getNumber setTitleColor: forState:UIControlStateNormal];
    getNumber.titleLabel.font = SYSTEMFONT(15);
    [getNumber addTapBlock:^(UIButton *btn) {
        [weakself getMoreAwardNum];
    }];
    [self.groundView addSubview:getNumber];
    
    [getNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.groundView.mas_centerX);
        make.bottom.equalTo(self.groundView.mas_bottom).offset(kRealHeightValue(-3));
        make.size.mas_equalTo(CGSizeMake(123, 30));
    }];
    
    [self.awardButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.groundView.mas_centerX);
        make.bottom.equalTo(getNumber.mas_top).offset(kRealHeightValue(-20));
        make.size.mas_equalTo(CGSizeMake(200, kRealHeightValue(36)));
    }];
}

-(void)getWheelADVData
{
    kWeakSelf(self);
    [self.viewModel getWheelRewardRecordInfoComplete:^(NSArray *data) {
        [weakself.numberArray addObjectsFromArray:data];
        [weakself setWheelADV];
    }];
}

#pragma mark -----跑马灯  轮播广告
-(void)setWheelADV
{
    //设置默认
    _index = 1;
    
    //第一次展示的是第一个广播
    if (self.numberArray.count > 0) {
        XLWheelRecordModel * model = self.numberArray[_index-1];
        self.rabaoLbl.text = [NSString stringWithFormat:@"%@获得%@",model.NickName,model.RewardDes];
    }
    
    //因为动画时间是三秒之后 所以 首先滚动的是下一个 也就是第二个
    if (self.numberArray.count > 1) {
         XLWheelRecordModel * model = self.numberArray[_index];
        self.nextRabaoLbl.text = [NSString stringWithFormat:@"%@获得%@",model.NickName,model.RewardDes];
        
        _index = 2;
    }
    
    //是否需要滚动轮播
    if (self.numberArray.count > 1) {
        //滚动间隔
        [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(runRabaoLabel) userInfo:nil repeats:YES];
    }
}



#pragma mark ----获取更多的抽奖机会
-(void)getMoreAwardNum
{
    XLUserInviteVC * vc = [[XLUserInviteVC alloc]init];
    [[self getCurrentUIVC].navigationController pushViewController:vc animated:YES];
    [self removeFromSuperview];
}

//绘制承载奖品
- (void)setRewardView
{
    UIImageView *rewardImgView = [[UIImageView alloc] init];
//    rewardImgView.image = [UIImage imageNamed:@"pic_zhuanpan_zhuan"];
//    rewardImgView.hidden = YES;
    [self.roundImgView addSubview:rewardImgView];
    
    [rewardImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.roundImgView.mas_centerX);
        make.centerY.equalTo(self.roundImgView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(kScale(270), kScale(270)));
    }];

    //绘制内容
    NSMutableArray * imageArray = [[NSMutableArray alloc]init];
    NSMutableArray * desLblArray = [[NSMutableArray alloc]init];
    for (WFManagerListModel * model in self.model.WFManagerList) {
        if([model.Money floatValue] > 0)
        {
            [imageArray addObject:@"pic_zhuanpan_dou_n"];
        }else
        {
            [imageArray addObject:@"pic_zhuanpan_ka_n"];
        }
        [desLblArray addObject:model.Description];
    }
    
    //获取底盘的高度
    CGFloat rewardHeight = kScale(270);
    
    //奖品个数
    NSInteger rewardCount = imageArray.count;
    
    [rewardImgView layoutIfNeeded];
    
    for (NSInteger i = 0; i < rewardCount; i ++) {
        
        UILabel *tempNumLbl = [[UILabel alloc] init];
        
        [rewardImgView addSubview:tempNumLbl];
        
        [tempNumLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(rewardImgView.mas_centerX);
            make.centerY.equalTo(rewardImgView.mas_centerY);
            make.width.mas_equalTo(rewardHeight/rewardCount * M_PI-kScale(10));
            make.height.mas_equalTo(rewardHeight-kScale(50));
        }];
        
        tempNumLbl.textAlignment = NSTextAlignmentCenter;
        
        tempNumLbl.font = [UIFont systemFontOfSize:14];
        
        //显示说明
        tempNumLbl.text = [NSString stringWithFormat:@"%@",desLblArray[i]];
        
        //设置锚点
        tempNumLbl.layer.anchorPoint = CGPointMake(0.5, 1);
        
        //计算角度
        CGFloat angle = M_PI * 2 / rewardCount * i;
        
        //旋转绘制
        tempNumLbl.transform = CGAffineTransformMakeRotation(angle);
        
        //绘制图片
        NSString *tempImgName = [NSString stringWithFormat:@"%@",imageArray[i]];
        
        UIImageView *rewardImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:tempImgName]];
        
        [tempNumLbl addSubview:rewardImgView];
        
        [rewardImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(tempNumLbl.mas_centerY).offset(kScale(25));
            make.centerX.equalTo(tempNumLbl.mas_centerX);
            make.width.mas_equalTo(kScale(46));
            make.height.mas_equalTo(kScale(46));
        }];
    }
}

//设置广告轮播
- (void)setRunLabel
{
    //设置喇叭背景
    self.rabaoView = [[UIView alloc] init];
    self.rabaoView.layer.cornerRadius = 4;
    self.rabaoView.layer.masksToBounds = YES;
    self.rabaoView.layer.borderColor = [UIColor colorWithRed:255/255.0 green:221/255.0 blue:2/255.0 alpha:1].CGColor;
    self.rabaoView.layer.borderWidth = 1;
    self.rabaoView.backgroundColor = [UIColor whiteColor];
    [self.groundView addSubview:self.rabaoView];
    
    [self.rabaoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topImgView.mas_bottom).offset(10);
        make.left.equalTo(self.groundView.mas_left).offset(kScale(15));
        make.width.mas_equalTo(184);
        make.height.mas_equalTo(24);
        
    }];
    
    //标志
    UIImageView *hornImgView = [[UIImageView alloc] init];
    hornImgView.image = [UIImage imageNamed:@"pic_zhuanpan_tongzhi_n copy"];
    [self.rabaoView addSubview:hornImgView];
    
    [hornImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rabaoView.mas_centerY);
        make.left.equalTo(self.rabaoView.mas_left).offset(kScale(5));
        make.width.height.mas_equalTo(kScale(12));
    }];
    
    //显示广播
    [self.rabaoView addSubview:self.rabaoLbl];
    
    [self.rabaoLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.rabaoView.mas_centerY);
        make.height.mas_equalTo(24);
        make.left.equalTo(hornImgView.mas_right).offset(kScale(12));
        make.right.equalTo(self.rabaoView.mas_right).offset(-3);
    }];
    
    //第二个广播位
    [self.rabaoView addSubview:self.nextRabaoLbl];
    
    [self.nextRabaoLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.rabaoView.mas_centerY).offset(24);
        make.left.equalTo(self.rabaoLbl);
        make.width.height.equalTo(self.rabaoLbl);
    }];
    
}

#pragma mark    - - - 方法事件 - - -

//点击旋转转盘
- (void)startAnimation:(UIButton *)sender
{
    //这里需要判断是否有抽奖资格
    if(_awardNum <= 0)
    {
        [MBProgressHUD showContent:@"抽奖次数已用完，点击获取更多抽奖机会"];
        return;
    }
    
    //防止动画没有结束就点击
    self.clickBtn.userInteractionEnabled = NO;
    self.awardButton.userInteractionEnabled = NO;
    [self.viewModel getWheelRewardInfoComplete:^(NSString *awardID) {
        //获取到的奖励
        self.numberIndex = 0;
        WFManagerListModel * selectModel;
        for (WFManagerListModel * model in self.model.WFManagerList) {
            self.numberIndex ++;
            if([model.Id isEqualToString:awardID])
            {
                selectModel = model;
                break;
            }
        }
        //转动圈数
        NSInteger rotateNum = 6;
        //旋转角度
        NSInteger rotateAngle;
        //根据得到的奖励 设置停留角度
        switch (self.numberIndex) {
            case 1:
            {
                rotateAngle = 0;
                break;
            }
            case 2:
            {
                rotateAngle = -60;
                break;
            }
            case 3:
            {
                rotateAngle = -120;
                break;
            }
            case 4:
            {
                rotateAngle = -180;
                break;
            }
            case 5:
            {
                rotateAngle = -240;
                break;
            }
            case 6:
            {
                rotateAngle = -300;
                break;
            }
                
                
            default:
                rotateAngle = 0;
                break;
        }
        
        //开始旋转
        CGFloat perAngle = M_PI/180.0;
        
        //转动时间
        CGFloat duration = 3.0f;
        
        //创建动画
        CABasicAnimation* rotationAnimation;
        
        //设置旋转轴
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        
        //旋转距离 时间等
        rotationAnimation.toValue = [NSNumber numberWithFloat:rotateAngle * perAngle + 360 * perAngle * rotateNum];
        rotationAnimation.duration = duration;
        rotationAnimation.cumulative = YES;
        rotationAnimation.delegate = self;
        //由快变慢
        rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        rotationAnimation.fillMode=kCAFillModeForwards;
        rotationAnimation.removedOnCompletion = NO;
        [self.roundImgView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
        
        
        _awardNum --;
        if(_awardNum > 0)
        {
            [self.awardButton setTitle:NSStringFormat(@"免费抽奖 %lu 次",_awardNum) forState:UIControlStateNormal];
        }else
        {
            [self.awardButton setTitle:@"获取免费次数" forState:UIControlStateNormal];
        }
        //防止一直点击
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.clickBtn.userInteractionEnabled = YES;
            self.awardButton.userInteractionEnabled = YES;
            if(selectModel)
            {
                BOOL isCard = YES;
                if([selectModel.Card integerValue] <= 0)
                {
                    isCard = NO;
                }
                NSString * content = @"";
                if(isCard)
                {
                    content = NSStringFormat(@"%@免费视频聊天卡",selectModel.Description);
                }else
                {
                    content = selectModel.Description;
                }
                XLWinningView * winView = [[XLWinningView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, KScreenHeight) IsCard:isCard AwardContent:content];
                [kAppWindow addSubview:winView];
            }
            //提示中的奖品
        });
    }];
    
    
    
    
}

//广播滚动
- (void)runRabaoLabel
{
    //是否有广告
    NSString *rabaoText = @"";
    
    NSString *nextText = @"";
    
    
    //是否超出所有广告个数 超出之后 重新播放
    if (_index >= self.numberArray.count+1) {
        
        _index = 1;
        
        //如果没有广告 为了防止意外问题，赋空值
        if (self.numberArray.count > 0) {
            XLWheelRecordModel * model = self.numberArray[_index-1];
            XLWheelRecordModel * nextModel = self.numberArray[_index];
            
            rabaoText = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@获得%@",model.NickName,model.RewardDes]];
            nextText = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@获得%@",nextModel.NickName,nextModel.RewardDes]];
            
        }else{
            rabaoText = @"";
        }
        
    }else{
        
        //计算下一个广播要比现在的多一个
        if (_index+1 >= self.numberArray.count) {
            
            XLWheelRecordModel * model = self.numberArray[0];
            nextText = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@获得%@",model.NickName,model.RewardDes]];
            
        }
        XLWheelRecordModel * model = self.numberArray[_index-1];
        rabaoText = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@获得%@",model.NickName,model.RewardDes]];
        
    }
    
    //根据奇偶性进行轮播识别
    //如果有广告 并且广告是两个以上， 那么就开始轮播， 如果一个以下 就不轮播
    if (self.numberArray.count > 1) {
        
        if (_index%2 == 1) {
            
            self.rabaoLbl.text = rabaoText;
            //更新播放位显示
            [self updateRabaoLocationByIsReadyNext:NO nextText:nextText];
            
        }else if (_index%2 == 0){
            
            self.nextRabaoLbl.text = rabaoText;
            [self updateRabaoLocationByIsReadyNext:YES nextText:nextText];
        }
        
    }

    _index++;
}

//更新广告位位置
- (void)updateRabaoLocationByIsReadyNext:(BOOL)next nextText:(NSString *)nextText
{
    
    //开始准备下一个
    if (!next) {

        //更新刚滚动上来的广告位
        [self.rabaoLbl mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.rabaoView.mas_centerY);
        }];
        
        [self.nextRabaoLbl mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.rabaoView.mas_centerY).offset(-48);
        }];
        
        [UIView animateWithDuration:1 animations:^{
            
            [[self.rabaoLbl superview] layoutIfNeeded];
            
        } completion:^(BOOL finished) {
            
            if (finished) {
                
                //动画结束之后 都重新设置在下方，下次继续往上轮播
                
                self.nextRabaoLbl.text = nextText;
                
                [self.nextRabaoLbl mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.rabaoView.mas_centerY).offset(24);
                }];
                
            }
            
        }];
        
    }else{
        
        //更新广告位
        [self.nextRabaoLbl mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.rabaoView.mas_centerY);
        }];
        
        [self.rabaoLbl mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.rabaoView.mas_centerY).offset(-48);
        }];
        
        [UIView animateWithDuration:1 animations:^{
            
            [[self.rabaoLbl superview] layoutIfNeeded];
            
        } completion:^(BOOL finished) {
            
            if (finished) {
                
                self.rabaoLbl.text = nextText;
                
                [self.rabaoLbl mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.centerY.equalTo(self.rabaoView.mas_centerY).offset(24);
                }];
                
            }
            
        }];

    }
}

#pragma mark    - - - lazy - - -

- (UIView *)groundView
{
    if (!_groundView) {
        self.groundView = [[UIImageView alloc] init];
        self.groundView.backgroundColor = KWhiteColor;
        self.groundView.layer.cornerRadius = 12;
        self.groundView.layer.masksToBounds = YES;
        self.groundView.userInteractionEnabled = YES;
    }
    
    return _groundView;
}

- (UIImageView *)roundImgView
{
    if (!_roundImgView) {
        self.roundImgView = [[UIImageView alloc] init];
        self.roundImgView.image = [UIImage imageNamed:@"pic_zhuanpan_pan_n copy"];
    }
    
    return _roundImgView;
}

- (UIImageView *)topImgView
{
    if (!_topImgView) {
        self.topImgView = [[UIImageView alloc] init];
        self.topImgView.image = [UIImage imageNamed:@"pic_zhuanpan_head_n copy"];
    }
    
    return _topImgView;
}

- (UILabel *)rabaoLbl
{
    if (!_rabaoLbl) {
        self.rabaoLbl = [[UILabel alloc] init];
        self.rabaoLbl.font = [UIFont systemFontOfSize:12];
        self.rabaoLbl.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        self.rabaoLbl.textAlignment = NSTextAlignmentLeft;
        self.rabaoLbl.text = @"";
    }
    
    return _rabaoLbl;
}

- (UILabel *)nextRabaoLbl
{
    if (!_nextRabaoLbl) {
        self.nextRabaoLbl = [[UILabel alloc] init];
        self.nextRabaoLbl.font = [UIFont systemFontOfSize:12];
        self.nextRabaoLbl.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1];
        self.nextRabaoLbl.textAlignment = NSTextAlignmentLeft;
        self.nextRabaoLbl.text = @"";
    }
    
    return _nextRabaoLbl;
}

-(UIButton *)awardButton
{
    if(_awardButton == nil)
    {
        _awardButton = [[UIButton alloc]init];
        [_awardButton setBackgroundImage:IMAGE_NAMED(@"pic_zhuanpan_but_n") forState:UIControlStateNormal];
        _awardButton.adjustsImageWhenHighlighted = NO;
        [_awardButton setTitleColor:RGB_HEX(0xC66516) forState:UIControlStateNormal];
        _awardButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:17];
        kWeakSelf(self);
        [_awardButton addTapBlock:^(UIButton *btn) {
            if(_awardNum > 0)
            {
                [weakself startAnimation:weakself.awardButton];
            }else
            {
                //获取更多的呃抽奖机会
                [weakself getMoreAwardNum];
            }
        }];
    }
    return _awardButton;
}


-(XLHomeViewModel *)viewModel
{
    if(_viewModel == nil)
    {
        _viewModel = [[XLHomeViewModel alloc]init];
    }
    return _viewModel;
}

-(NSMutableArray *)numberArray
{
    if(_numberArray == nil)
    {
        _numberArray = [[NSMutableArray alloc]init];
    }
    return _numberArray;
}

-(UIViewController *)getCurrentVC{
    
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}

-(UIViewController *)getCurrentUIVC
{
    UIViewController  *superVC = [self getCurrentVC];
    
    if ([superVC isKindOfClass:[UITabBarController class]]) {
        
        UIViewController  *tabSelectVC = ((UITabBarController*)superVC).selectedViewController;
        
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            
            return ((UINavigationController*)tabSelectVC).viewControllers.lastObject;
        }
        return tabSelectVC;
    }else
        if ([superVC isKindOfClass:[UINavigationController class]]) {
            
            return ((UINavigationController*)superVC).viewControllers.lastObject;
        }
    return superVC;
}









@end
